package com.wheel.mybatis.typeHandler;


import com.wheel.core.utils.Log;
import org.apache.ibatis.type.*;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Alias("stringDateHandler")
@MappedTypes(String.class)
@MappedJdbcTypes({JdbcType.TIMESTAMP, JdbcType.DATE})
public class StringDateTypeHandler extends BaseTypeHandler<String> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        Date convert = convert(parameter);
        ps.setTimestamp(i, new Timestamp(convert.getTime()));
    }

    private static Date convert(String value) {
        if (value == null) {
            return null;
        } else {
            String s = (String) value;
            String format = "";
            if (s.length() == 0) {
                return null;
            } else if (s.length() == 10 && s.matches("(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)")) {
                format = "yyyy-MM-dd";
            } else if (s.length() == 8 && s.matches("^([0-1]?\\d|2[0-3]):([0-5]?\\d):([0-5]?\\d)$")) {
                format = "HH:mm:ss";
            } else if (s.length() == 19) {
                format = "yyyy-MM-dd HH:mm:ss";
            } else if (s.length() == 16) {
                format = "yyyy-MM-dd HH:mm";
            } else if (s.length() == 23) {
                format = "yyyy-MM-dd HH:mm:ss.SSS";
            } else {
                throw new RuntimeException(Log.formatMessage("{} not date String",s));
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            try {
                return sdf.parse(s);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
    }


    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        Timestamp sqlTimestamp = rs.getTimestamp(columnName);
        if (sqlTimestamp != null) {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sqlTimestamp);
        }
        return null;
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        Timestamp sqlTimestamp = rs.getTimestamp(columnIndex);
        if (sqlTimestamp != null) {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sqlTimestamp);
        }
        return null;
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        Timestamp sqlTimestamp = cs.getTimestamp(columnIndex);
        if (sqlTimestamp != null) {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sqlTimestamp);
        }
        return null;
    }


}